package bar.dark.lee.setting.viewtype;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;
import bar.dark.lee.BDConstents;
import bar.dark.lee.R;
import bar.dark.lee.SuperActivity;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.common.RadioDialogActivity;
import bar.dark.lee.common.SuperInterface;
import bar.dark.lee.setting.TipInfoActivity;
import bar.dark.lee.setting.TipsActivity;
import bar.dark.lee.widget.WidgetArrayList;
import bar.dark.lee.widget.WidgetGetSqlQuery;

public class WidgetViewTypeActivity extends SuperActivity implements SuperInterface
{
	private static final int REQUEST_WIDGET1 = 0;
	private static final int REQUEST_WIDGET2 = 1;
	private static final int REQUEST_WIDGET3 = 2;
	private static final int REQUEST_WIDGET4 = 3;
	private static final int REQUEST_WIDGET_CLEAR = 4;

	private WidgetArrayList widgetList;
	
	private SharedPreferences pref;
	private SharedPreferences.Editor editor;
	
	/** 3x2 라디오 버튼 */
	private RadioButton widgetRadio3x2;
	/** 3x1 라디오 버튼 */
	private RadioButton widgetRadio3x1;
	
	private View widgetView1;
	private TextView widgetTextView1;
	private View widgetView2;
	private TextView widgetTextView2;
	private View widgetView3;
	private TextView widgetTextView3;
	private View widgetView4;
	private TextView widgetTextView4;
	
	/** 라디오 버튼에 따른 위젯 보이게 안보이게 하는 뷰*/
	private View widgetFlagView1;
	/** 라디오 버튼에 따른 위젯 보이게 안보이게 하는 뷰*/
	private View widgetFlagView2;
	
	private View widgetClearView;
	private TextView widgetClearTextView;
	
	private Button widgetButton;
	
	private String [] bigKey;
	private String [] smallKey;
	
	private String widgetClearKey;
	
	private WidgetGetSqlQuery widgetSql;
	
	private ArrayList<String> clearList;
	
	private String widget1_key = "-1";
	private String widget2_key = "-1";
	private String widget3_key = "-1";
	private String widget4_key = "-1";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_widgetviewtype_activity);
		
		setTitle("위젯 설정");
		
		setFindView();
		setClickListener();
		
		widgetSql = new WidgetGetSqlQuery(this);
		
		new ShowDialog().execute(BDUtil.asyncFlag, 0);
	}
	
	@Override
	protected void tipButton() 
	{
		Intent intent = new Intent(WidgetViewTypeActivity.this, TipInfoActivity.class);
		intent.putExtra(TipsActivity.TIP_REQUEST_KEY, TipsActivity.TIP_VIEWTYPE);
		startActivity(intent);
	}
	
	@Override
	public void setFindView() 
	{
		widgetRadio3x2 = (RadioButton) findViewById(R.id.settingWidgetRadio3x2);
		widgetRadio3x1 = (RadioButton) findViewById(R.id.settingWidgetRadio3x1);
		
		widgetView1 = findViewById(R.id.settingWidgetView1);
		widgetView2 = findViewById(R.id.settingWidgetView2);
		widgetView3 = findViewById(R.id.settingWidgetView3);
		widgetView4 = findViewById(R.id.settingWidgetView4);
		
		widgetTextView1 = (TextView) findViewById(R.id.settingWidgetTextView1);
		widgetTextView2 = (TextView) findViewById(R.id.settingWidgetTextView2);
		widgetTextView3 = (TextView) findViewById(R.id.settingWidgetTextView3);
		widgetTextView4 = (TextView) findViewById(R.id.settingWidgetTextView4);
		
		widgetFlagView1 = findViewById(R.id.settingWidgetFlagView1);
		widgetFlagView2 = findViewById(R.id.settingWidgetFlagView2);
		
		widgetClearView = findViewById(R.id.settingWidgetClearView);
		widgetClearTextView = (TextView) findViewById(R.id.settingWidgetClearTextView);
		
		widgetButton = (Button) findViewById(R.id.settingWidgetButton);
	}
	
	@Override
	public void setClickListener() 
	{
		widgetRadio3x2.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				widgetFlagView1.setVisibility(View.VISIBLE);
				widgetFlagView2.setVisibility(View.VISIBLE);
				
				widgetTextView1.setText(widgetSql.getName(bigKey[0], 0, 0));
				widgetTextView2.setText(widgetSql.getName(bigKey[1], 1, 0));
				widgetTextView3.setText(widgetSql.getName(bigKey[2], 2, 0));
				widgetTextView4.setText(widgetSql.getName(bigKey[3], 3, 0));
				
				setBigWidgetKey();
			}
		});
		
		widgetRadio3x1.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				widgetFlagView1.setVisibility(View.GONE);
				widgetFlagView2.setVisibility(View.GONE);
				
				widgetTextView1.setText(widgetSql.getName(smallKey[0], 4, 0));
				widgetTextView2.setText(widgetSql.getName(smallKey[1], 5, 0));
				
				setSmallWidgetKey();
			}
		});
		
		widgetView1.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
//				Intent intent = new Intent(WidgetViewTypeActivity.this, RadioDialogActivity.class);
//				intent.putExtra(RadioDialogActivity.RADIODATA, widgetList);
//				intent.putExtra(RadioDialogActivity.RADIOSELECT, widgetTextView1.getText().toString());
//				startActivityForResult(intent, REQUEST_WIDGET1);
				
				Intent intent = new Intent(WidgetViewTypeActivity.this, WidgetDialogActivity.class);
				intent.putExtra(WidgetDialogActivity.REQUEST_CODE, REQUEST_WIDGET1);
				intent.putExtra(WidgetDialogActivity.RADIOSELECT, widgetTextView1.getText().toString());
				startActivityForResult(intent, REQUEST_WIDGET1);
			}
		});
		
		widgetView2.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
//				Intent intent = new Intent(WidgetViewTypeActivity.this, RadioDialogActivity.class);
//				intent.putExtra(RadioDialogActivity.RADIODATA, widgetList);
//				intent.putExtra(RadioDialogActivity.RADIOSELECT, widgetTextView2.getText().toString());
//				startActivityForResult(intent, REQUEST_WIDGET2);
				
				Intent intent = new Intent(WidgetViewTypeActivity.this, WidgetDialogActivity.class);
				intent.putExtra(WidgetDialogActivity.REQUEST_CODE, REQUEST_WIDGET2);
				intent.putExtra(WidgetDialogActivity.RADIOSELECT, widgetTextView2.getText().toString());
				startActivityForResult(intent, REQUEST_WIDGET2);
			}
		});
		
		widgetView3.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
//				Intent intent = new Intent(WidgetViewTypeActivity.this, RadioDialogActivity.class);
//				intent.putExtra(RadioDialogActivity.RADIODATA, widgetList);
//				intent.putExtra(RadioDialogActivity.RADIOSELECT, widgetTextView3.getText().toString());
//				startActivityForResult(intent, REQUEST_WIDGET3);
				
				Intent intent = new Intent(WidgetViewTypeActivity.this, WidgetDialogActivity.class);
				intent.putExtra(WidgetDialogActivity.REQUEST_CODE, REQUEST_WIDGET3);
				intent.putExtra(WidgetDialogActivity.RADIOSELECT, widgetTextView3.getText().toString());
				startActivityForResult(intent, REQUEST_WIDGET3);
			}
		});
		
		widgetView4.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
//				Intent intent = new Intent(WidgetViewTypeActivity.this, RadioDialogActivity.class);
//				intent.putExtra(RadioDialogActivity.RADIODATA, widgetList);
//				intent.putExtra(RadioDialogActivity.RADIOSELECT, widgetTextView4.getText().toString());
//				startActivityForResult(intent, REQUEST_WIDGET4);
				
				Intent intent = new Intent(WidgetViewTypeActivity.this, WidgetDialogActivity.class);
				intent.putExtra(WidgetDialogActivity.REQUEST_CODE, REQUEST_WIDGET4);
				intent.putExtra(WidgetDialogActivity.RADIOSELECT, widgetTextView4.getText().toString());
				startActivityForResult(intent, REQUEST_WIDGET4);
			}
		});
		
		widgetClearView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(WidgetViewTypeActivity.this, RadioDialogActivity.class);
				intent.putExtra(RadioDialogActivity.RADIODATA, clearList);
				intent.putExtra(RadioDialogActivity.RADIOSELECT, widgetClearTextView.getText().toString());
				startActivityForResult(intent, REQUEST_WIDGET_CLEAR);
			}
		});
		
		widgetButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				BDUtil.showSelectDialog(WidgetViewTypeActivity.this, "알림", "위젯 표시항목을 변경 하시겠습니까?", 
						new DialogInterface.OnClickListener() 
				{
					@Override
					public void onClick(DialogInterface dialog, int which) 
					{
						new ShowDialog().execute(BDUtil.asyncFlag, 2);
						dialog.dismiss();
					}
				});
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode == Activity.RESULT_OK)
		{
			String selectData = data.getStringExtra(RadioDialogActivity.RADIOSTRING);
			
			if (selectData.equals("없음"))
			{
				BDUtil.showToast(superContext, "표시할 항목이 없습니다. 다시 선택해 주세요.");
				return;
			}
			
			if (requestCode == REQUEST_WIDGET1)
			{
				widget1_key = data.getStringExtra(WidgetDialogActivity.WIDGET_TYPE_KEY);
				widgetTextView1.setText(selectData);
			}
			else if (requestCode == REQUEST_WIDGET2)
			{
				widget2_key = data.getStringExtra(WidgetDialogActivity.WIDGET_TYPE_KEY);
				widgetTextView2.setText(selectData);
			}
			else if (requestCode == REQUEST_WIDGET3)
			{
				widget3_key = data.getStringExtra(WidgetDialogActivity.WIDGET_TYPE_KEY);
				widgetTextView3.setText(selectData);
			}
			else if (requestCode == REQUEST_WIDGET4)
			{
				widget4_key = data.getStringExtra(WidgetDialogActivity.WIDGET_TYPE_KEY);
				widgetTextView4.setText(selectData);
			}
			else if (requestCode == REQUEST_WIDGET_CLEAR)
			{
				widgetClearTextView.setText(selectData);
			}
		}
	}
	
	@Override
	public void setDataSetting() 
	{
		pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		editor = pref.edit();
		
		widgetList = new WidgetArrayList();
		
		String bigWidget = pref.getString(BDConstents.BIG_WIDGET_SETTING, "0,1,2,3"); 
		String smallWidget = pref.getString(BDConstents.SMALL_WIDGET_SETTING, "4,5");
		
		bigKey = bigWidget.split(",");
		smallKey = smallWidget.split(",");
		
		if (bigKey.length < 4 )
		{
			bigWidget = "0,1,2,3";
			bigKey = bigWidget.split(",");
		}
		
		if (smallKey.length < 2)
		{
			smallWidget = "4,5";
			smallKey = smallWidget.split(",");
		}
		
		setBigWidgetKey();
		
		widgetClearKey = pref.getString(BDConstents.WIDGET_CLEAR_SETTING, "0");
		
		clearList = new ArrayList<String>();
		clearList.add("불투명");
		clearList.add("투명 1단계");
		clearList.add("투명 2단계");
		clearList.add("투명 3단계");
		clearList.add("투명");
	}

	private void setBigWidgetKey()
	{
		if (bigKey[0].equals(WidgetGetSqlQuery.BANKBOOK)
			|| bigKey[0].equals(WidgetGetSqlQuery.CHECKCARD)
			|| bigKey[0].equals(WidgetGetSqlQuery.CREDITCARD))
		{
			widget1_key = bigKey[0];
		}
		else
		{
			widget1_key = "-1";
		}
		
		if (bigKey[1].equals(WidgetGetSqlQuery.BANKBOOK)
				|| bigKey[1].equals(WidgetGetSqlQuery.CHECKCARD)
				|| bigKey[1].equals(WidgetGetSqlQuery.CREDITCARD))
		{
			widget2_key = bigKey[1];
		}
		else
		{
			widget2_key = "-1";
		}
		
		if (bigKey[2].equals(WidgetGetSqlQuery.BANKBOOK)
				|| bigKey[2].equals(WidgetGetSqlQuery.CHECKCARD)
				|| bigKey[2].equals(WidgetGetSqlQuery.CREDITCARD))
		{
			widget3_key = bigKey[2];
		}
		else
		{
			widget3_key = "-1";
		}
		
		if (bigKey[3].equals(WidgetGetSqlQuery.BANKBOOK)
				|| bigKey[3].equals(WidgetGetSqlQuery.CHECKCARD)
				|| bigKey[3].equals(WidgetGetSqlQuery.CREDITCARD))
		{
			widget4_key = bigKey[3];
		}
		else
		{
			widget4_key = "-1";
		}
	}
	
	private void setSmallWidgetKey()
	{
		if (smallKey[0].equals(WidgetGetSqlQuery.BANKBOOK)
			|| smallKey[0].equals(WidgetGetSqlQuery.CHECKCARD)
			|| smallKey[0].equals(WidgetGetSqlQuery.CREDITCARD))
		{
			widget1_key = smallKey[0];
		}
		else
		{
			widget1_key = "-1";
		}
		
		if (smallKey[1].equals(WidgetGetSqlQuery.BANKBOOK)
				|| smallKey[1].equals(WidgetGetSqlQuery.CHECKCARD)
				|| smallKey[1].equals(WidgetGetSqlQuery.CREDITCARD))
		{
			widget2_key = smallKey[1];
		}
		else
		{
			widget2_key = "-1";
		}
	}
	
	private void saveWidget()
	{
		if (widgetRadio3x2.isChecked())
		{
			String key = "";
			
			if (widget1_key.equals("-1"))
			{
				for (int i = 0; i < widgetList.size(); i++)
				{
					String name = widgetList.get(i);
					
					if (widgetTextView1.getText().toString().equals(name))
					{
						key += i + ",";
					}
				}
				
				editor.putString(BDConstents.BIG_WIDGET_MONEYKIND1, "");
			}
			else
			{
				key += widget1_key + ",";
				editor.putString(BDConstents.BIG_WIDGET_MONEYKIND1, 
						widgetTextView1.getText().toString());
			}
			
			if (widget2_key.equals("-1"))
			{
				for (int i = 0; i < widgetList.size(); i++)
				{
					String name = widgetList.get(i);
					
					if (widgetTextView2.getText().toString().equals(name))
					{
						key += i + ",";
					}
				}
				
				editor.putString(BDConstents.BIG_WIDGET_MONEYKIND2, "");
			}
			else
			{
				key += widget2_key + ",";
				editor.putString(BDConstents.BIG_WIDGET_MONEYKIND2, 
						widgetTextView2.getText().toString());
			}
			
			if (widget3_key.equals("-1"))
			{
				for (int i = 0; i < widgetList.size(); i++)
				{
					String name = widgetList.get(i);
					
					if (widgetTextView3.getText().toString().equals(name))
					{
						key += i + ",";
					}
				}
				
				editor.putString(BDConstents.BIG_WIDGET_MONEYKIND3, "");
			}
			else
			{
				key += widget3_key + ",";
				editor.putString(BDConstents.BIG_WIDGET_MONEYKIND3, 
						widgetTextView3.getText().toString());
			}
			
			if (widget4_key.equals("-1"))
			{
				for (int i = 0; i < widgetList.size(); i++)
				{
					String name = widgetList.get(i);
					
					if (widgetTextView4.getText().toString().equals(name))
					{
						key += i + "";
					}
				}
				
				editor.putString(BDConstents.BIG_WIDGET_MONEYKIND4, "");
			}
			else
			{
				key += widget4_key + "";
				editor.putString(BDConstents.BIG_WIDGET_MONEYKIND4, 
						widgetTextView4.getText().toString());
			}
			
			editor.putString(BDConstents.BIG_WIDGET_SETTING, key);
		}
		else
		{
			String key = "";
			
			if (widget1_key.equals("-1"))
			{
				for (int i = 0; i < widgetList.size(); i++)
				{
					String name = widgetList.get(i);
					
					if (widgetTextView1.getText().toString().equals(name))
					{
						key += i + ",";
					}
				}
				
				editor.putString(BDConstents.SMALL_WIDGET_MONEYKIND1, "");
			}
			else
			{
				key += widget1_key + ",";
				editor.putString(BDConstents.SMALL_WIDGET_MONEYKIND1, 
						widgetTextView1.getText().toString());
			}
			
			if (widget2_key.equals("-1"))
			{
				for (int i = 0; i < widgetList.size(); i++)
				{
					String name = widgetList.get(i);
					
					if (widgetTextView2.getText().toString().equals(name))
					{
						key += i + "";
					}
				}
				
				editor.putString(BDConstents.SMALL_WIDGET_MONEYKIND2, "");
			}
			else
			{
				key += widget2_key + "";
				editor.putString(BDConstents.SMALL_WIDGET_MONEYKIND2, 
						widgetTextView2.getText().toString());
			}
			
			editor.putString(BDConstents.SMALL_WIDGET_SETTING, key);
		}
		
		String widgetClearText = widgetClearTextView.getText().toString();
		
		for (int i = 0; i < clearList.size(); i++)
		{
			if (widgetClearText.equals(clearList.get(i)))
			{
				editor.putString(BDConstents.WIDGET_CLEAR_SETTING, i + "");
				break;
			}
		}
		
		editor.commit();
	}
	
	/**
	 * 다이얼로그 뛰우기 
	 */
	private class ShowDialog extends AsyncTask<Integer, Integer, Integer>{
		ProgressDialog dialog = null;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
			if (BDUtil.btnEnable)
			{
				BDUtil.btnEnable = false;
				BDUtil.asyncFlag = -1;
				
				dialog = new ProgressDialog(WidgetViewTypeActivity.this);
				dialog.setCancelable(false);
				dialog.setMessage("Loading...");
				dialog.show();
			}
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) {
			if (arg0[0] == 0)
			{
				if (arg0[1] == 0)
				{	// 초기셋팅
					setDataSetting();
					return 0;
				}
				else if (arg0[1] == 2)
				{	// 위젯 저장
					saveWidget();
					return 2;
				}
			}
			
			return null;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			
			dialog.dismiss();
			
			if (result == 0)
			{
				BDUtil.btnEnable = true;
				BDUtil.asyncFlag = 0;
				
				widgetTextView1.setText(widgetSql.getName(bigKey[0], 0, 0));
				widgetTextView2.setText(widgetSql.getName(bigKey[1], 1, 0));
				widgetTextView3.setText(widgetSql.getName(bigKey[2], 2, 0));
				widgetTextView4.setText(widgetSql.getName(bigKey[3], 3, 0));
				
				int widgetClearFlag = 0;
				
				try
				{
					widgetClearFlag = Integer.parseInt(widgetClearKey);
				} catch (NumberFormatException e)
				{
					widgetClearFlag = 0;
				}
				
				widgetClearTextView.setText(clearList.get(widgetClearFlag));
			}
			else if (result == 2)
			{	// 위젯 저장
				BDUtil.showDialogFinish(WidgetViewTypeActivity.this, "알림", "위젯 설정이 변경되었습니다.");
			}
		}
	}
}
